<template>
    <div class="goodsInfo-container">

            <transition
            @before-enter="beforeEnter"
            @enter="enter"
            @after-enter="afterEnter"
            >
                <div class="ball" v-show="ballFlag" ref="ball"></div>
            </transition>
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <swiper :lunbotuList="lunbotu" :isfull="false"></swiper>
                </div>
            </div>
        </div>

        <div class="mui-card">
            <div class="mui-card-header">{{goodsInfo.title}}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>市场价:<span class="old">￥{{goodsInfo.market_price}}</span>&nbsp;&nbsp;&nbsp;销售价:<span class="now">￥{{goodsInfo.sell_price}}</span></p>
                    <p>购买数量：<numbox @getcount="getSelectedCount" :max="goodsInfo.stock_quantity"></numbox></p>
                    <mt-button type="primary" size="small">立即购买</mt-button>
                    <mt-button type="danger" size="small" @click="addTOShopCar">加入购物车</mt-button>
                </div>
            </div>
        </div>
        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                   <p>商品货号:{{goodsInfo.goods_no}}</p>
                   <p>库存情况:{{goodsInfo.stock_quantity}}件</p>
                   <p>上架时间:{{goodsInfo.add_time | dateFormat}}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button size="large" type="primary" plain @click="introduce(id)">图文介绍</mt-button>
                <mt-button size="large" type="danger" plain @click="comment(id)">商品评论</mt-button>
            </div>
        </div>
    </div>
</template>

<script>
    import swiper from '../subcomponents/swiper.vue'
    import numbox from '../subcomponents/goodsInfo_numbox.vue'
    export default {
        data(){
            return{
                id:this.$route.params.id,
                lunbotu:[],
                goodsInfo:{},
                ballFlag:false,
                selectedCount:1
            }
        },
        created(){
            this.getLunbotu();
            this.getGoodsInfo();
        },
        methods:{
            getLunbotu(){
                this.$http.get('http://www.liulongbin.top:3005/api/getthumimages/' + this.id).then(result =>{
                    var result = result.body;
                    if(result){
                        result.message.forEach(item =>{
                            item.img = item.src;
                        });
                        this.lunbotu = result.message;

                    }
                })
            },
            getGoodsInfo(){
                this.$http.get('http://www.liulongbin.top:3005/api/goods/getInfo/' + this.id).then(result =>{
                    var result = result.body;
                    if(result){
                        this.goodsInfo = result.message[0];
                    }
                })
            },
            introduce(id){
                this.$router.push('/home/goodsintroduce/' + id);
            },
            comment(id){
                this.$router.push('/home/goodscomment/' + id);
            },
            addTOShopCar(){
                this.ballFlag = !this.ballFlag;

                var goodsinfo = {
                    id:this.id,
                    count:this.selectedCount,
                    price:this.goodsInfo.sell_price,
                    selected:true
                };
                this.$store.commit('addToCar',goodsinfo);

            },
            beforeEnter(el){
                el.style.transform = "translate(0,0)"
            },
            enter(el,done){
                var ballPosition = this.$refs.ball.getBoundingClientRect();
                var badgePosition = document.getElementById("badge").getBoundingClientRect();
                var xdist = badgePosition.left - ballPosition.left;
                var ydist = badgePosition.top - ballPosition.top;
                el.offsetWidth;
                el.style.transform = "translate("+xdist+"px,"+ydist+"px)";
                el.style.transition = "all 0.5s cubic-bezier(0,-0.3,1,.68)";
                done()
            },
            afterEnter(el){
                this.ballFlag = !this.ballFlag;
            },
            getSelectedCount(count){
                this.selectedCount = count;
                console.log("拿到父组件的值为"+ this.selectedCount);
            }
        },
        components:{
            swiper,
            numbox
        }
    }
</script>

<style scoped>
 .goodsInfo-container{
     background: #eee;
     overflow: hidden;
 }
 .old{
     text-decoration: line-through;
 }
 .now{
     font-weight:bold;
     color: red;
 }
 .mui-card-footer{
        display: block;
    }
 .mui-card-footer button{
     margin: 15px 0;
 }
    .ball{
        width: 15px;
        height: 15px;
        background: red;
        border-radius:50%;
        position: absolute;
        z-index: 99;
        top: 390px;
        left:148px;

 }
</style>